package controller

import (
	"RocFamilySystem/internal/service"
	"RocFamilySystem/models"
	"RocFamilySystem/pkg/constant"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

type GeneratorTableController struct {
	BaseController
	GeneratorTableService *service.GeneratorTableService
}

// 初始化
func init() {
	registerContainer["GeneratorTableController"] = &GeneratorTableController{}
}

// SetService 设置服务
func (controller *GeneratorTableController) SetService(sysGeneratorTableService *service.GeneratorTableService) {
	controller.GeneratorTableService = sysGeneratorTableService
}

// RegisterRouter 注册路由
func (controller *GeneratorTableController) RegisterRouter(engine *gin.Engine) {
	prefix := "/generator/table"
	g := engine.Group(prefix)
	prefixPermission := "permission" + strings.ReplaceAll(prefix, "/", ":")
	{
		//新增生成表
		g.POST("", controller.AddGeneratorTable)
		registerPermissionContainer("新增生成表", prefixPermission+":create", prefix, "POST")
		//删除生成表
		g.DELETE("/:id", controller.RemoveGeneratorTable)
		registerPermissionContainer("删除生成表", prefixPermission+":delete", prefix, "DELETE")
		//修改生成表
		g.PUT("", controller.ModifyGeneratorTable)
		registerPermissionContainer("修改生成表", prefixPermission+":update", prefix, "PUT")
		//获取生成表列表
		g.POST("/list", controller.QueryGeneratorTable)
		registerPermissionContainer("获取生成表列表", prefixPermission+":list", prefix+"/list", "POST")
		//通过ID查询生成表
		g.GET("/:id", controller.QueryGeneratorTableById)
		registerPermissionContainer("查询Id生成表", prefixPermission+":id", prefix+"/:id", "GET")
		//预览生成代码
		g.GET("/preview", controller.GeneratorCodePreview)
		registerPermissionContainer("预览生成表", prefixPermission+":preview", prefix+"/preview", "GET")
		//下载代码
		g.GET("/download", controller.GeneratorCodeDownload)
		registerPermissionContainer("下载生成表代码", prefixPermission+":download", prefix+"/download", "GET")
	}
}

// AddGeneratorTable 添加生成表
// @Summary AddGeneratorTable 添加生成表
// @Tags 生成表
// @Produce json
// @Param sysGeneratorTable body models.GeneratorTable true "生成表"
// @Success 200 {object} Result "成功"
// @Router /generator/table [post]
func (controller *GeneratorTableController) AddGeneratorTable(ctx *gin.Context) {
	var sysGeneratorTable models.GeneratorTable
	err := ctx.ShouldBind(&sysGeneratorTable)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.GeneratorTableService.AddGeneratorTable(&sysGeneratorTable)
	controller.CustomResult(ctx, 200, "添加生成表成功", nil)
}

// RemoveGeneratorTable 删除生成表
// @Summary RemoveGeneratorTable  删除生成表
// @Tags 生成表
// @Produce json
// @Param id path int true "生成表id"
// @Success 200 {object} Result "成功"
// @Router /generator/table/:id [delete]
func (controller *GeneratorTableController) RemoveGeneratorTable(ctx *gin.Context) {
	sysGeneratorTableId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.GeneratorTableService.RemoveGeneratorTable(sysGeneratorTableId)
	controller.CustomResult(ctx, 200, fmt.Sprintf("删除【%d】成功", sysGeneratorTableId), nil)
}

// ModifyGeneratorTable 修改生成表
// @Summary ModifyGeneratorTable 修改生成表
// @Tags 生成表
// @Produce json
// @Param sysGeneratorTable body models.GeneratorTable true "生成表"
// @Success 200 {object} Result "成功"
// @Router /generator/table [put]
func (controller *GeneratorTableController) ModifyGeneratorTable(ctx *gin.Context) {
	var sysGeneratorTable models.GeneratorTable
	err := ctx.ShouldBind(&sysGeneratorTable)
	controller.HandleError(err, constant.PARAMS_ERROR)
	controller.GeneratorTableService.ModifyGeneratorTable(&sysGeneratorTable)
	controller.CustomResult(ctx, 200, fmt.Sprintf("修改【%d】成功", *sysGeneratorTable.Id), nil)
}

// QueryGeneratorTable 查询生成表
// @Summary QueryGeneratorTable 修改生成表
// @Tags 生成表
// @Produce json
// @Param sysGeneratorTable body models.GeneratorTable true "生成表"
// @Success 200 {object} Result "成功"
// @Router /generator/table/list [post]
func (controller *GeneratorTableController) QueryGeneratorTable(ctx *gin.Context) {
	pageNum, pageSize := controller.GetPageInfo(ctx)
	var sysGeneratorTable models.GeneratorTable
	err := ctx.ShouldBind(&sysGeneratorTable)
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.GeneratorTableService.QueryGeneratorTable(&sysGeneratorTable, pageNum, pageSize)
	controller.SuccessResult(ctx, data)
}

// QueryGeneratorTableById 查询生成表通过ID
// @Summary QueryGeneratorTableById  查询生成表通过ID
// @Tags 生成表
// @Produce json
// @Param id path int true "生成表id"
// @Success 200 {object} Result "成功"
// @Router /generator/table/:id [get]
func (controller *GeneratorTableController) QueryGeneratorTableById(ctx *gin.Context) {
	sysGeneratorTableId, err := strconv.Atoi(ctx.Param("id"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.GeneratorTableService.QueryGeneratorTableById(sysGeneratorTableId)
	controller.SuccessResult(ctx, data)
}

// GeneratorCodePreview 生成代码预览
// @Summary GeneratorCodePreview  生成代码预览
// @Tags 生成表
// @Produce json
// @Param tableId query int true "生成表id"
// @Param templateID query int true "生成模板id"
// @Success 200 {object} Result "成功"
// @Router /generator/table/preview [get]
func (controller *GeneratorTableController) GeneratorCodePreview(ctx *gin.Context) {
	tableId, err := strconv.Atoi(ctx.Query("tableId"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	templateId, err := strconv.Atoi(ctx.Query("templateId"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.GeneratorTableService.GeneratorCodePreview(tableId, templateId)
	controller.SuccessResult(ctx, data)
}

// GeneratorCodeDownload 代码下载
// @Summary GeneratorCodeDownload   代码下载
// @Tags 生成表
// @Produce json
// @Param tableId query int true "生成表id"
// @Param templateID query int true "生成模板id"
// @Success 200 {object} Result "成功"
// @Router /generator/table/download [get]
func (controller *GeneratorTableController) GeneratorCodeDownload(ctx *gin.Context) {
	tableId, err := strconv.Atoi(ctx.Query("tableId"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	templateId, err := strconv.Atoi(ctx.Query("templateId"))
	controller.HandleError(err, constant.PARAMS_ERROR)
	data := controller.GeneratorTableService.GeneratorCodeDownload(tableId, templateId)
	ctx.Header("Content-Type", "application/octet-stream")
	ctx.Header("Content-Disposition", "attachment;filename=Rocc.zip")
	ctx.Header("Content-Transfer-Encoding", "binary")

	if _, err = ctx.Writer.Write(data.Bytes()); err != nil {
		controller.FailResult(ctx, "下载生成代码失败！")
	}
}
